package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Tipoexamen;
import com.lostreseditores.sabersai.model.dto.TipoexamenDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class TipoexamenView {
    private InputText txtTipoexamdescript;
    private InputText txtTipoexamidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<TipoexamenDTO> data;
    private TipoexamenDTO selectedTipoexamen;

    public TipoexamenView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            TipoexamenDTO tipoexamenDTO = (TipoexamenDTO) e.getObject();

            if (txtTipoexamdescript == null) {
                txtTipoexamdescript = new InputText();
            }

            txtTipoexamdescript.setValue(tipoexamenDTO.getTipoexamdescript());

            if (txtTipoexamidn == null) {
                txtTipoexamidn = new InputText();
            }

            txtTipoexamidn.setValue(tipoexamenDTO.getTipoexamidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtTipoexamdescript != null) {
            txtTipoexamdescript.setValue(null);
            txtTipoexamdescript.setDisabled(true);
        }

        if (txtTipoexamidn != null) {
            txtTipoexamidn.setValue(null);
            txtTipoexamidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Tipoexamen entity = null;

        try {
            Integer tipoexamidn = new Integer(txtTipoexamidn.getValue()
                                                            .toString());
            entity = BusinessDelegatorView.getTipoexamen(tipoexamidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtTipoexamdescript.setDisabled(false);
            txtTipoexamidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtTipoexamdescript.setValue(entity.getTipoexamdescript());
            txtTipoexamdescript.setDisabled(false);
            txtTipoexamidn.setValue(entity.getTipoexamidn());
            txtTipoexamidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveTipoexamen(FacesUtils.checkString(
                    txtTipoexamdescript),
                FacesUtils.checkInteger(txtTipoexamidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteTipoexamen(FacesUtils.checkInteger(
                    txtTipoexamidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateTipoexamen(FacesUtils.checkString(
                    txtTipoexamdescript),
                FacesUtils.checkInteger(txtTipoexamidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtTipoexamidn == null) {
                txtTipoexamidn = new InputText();
            }

            txtTipoexamidn.setValue(selectedTipoexamen.getTipoexamidn());

            BusinessDelegatorView.deleteTipoexamen(FacesUtils.checkInteger(
                    txtTipoexamidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedTipoexamen);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String tipoexamdescript,
        Integer tipoexamidn) throws Exception {
        try {
            BusinessDelegatorView.updateTipoexamen(tipoexamdescript, tipoexamidn);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("TipoexamenView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtTipoexamdescript() {
        return txtTipoexamdescript;
    }

    public void setTxtTipoexamdescript(InputText txtTipoexamdescript) {
        this.txtTipoexamdescript = txtTipoexamdescript;
    }

    public InputText getTxtTipoexamidn() {
        return txtTipoexamidn;
    }

    public void setTxtTipoexamidn(InputText txtTipoexamidn) {
        this.txtTipoexamidn = txtTipoexamidn;
    }

    public List<TipoexamenDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataTipoexamen();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<TipoexamenDTO> tipoexamenDTO) {
        this.data = tipoexamenDTO;
    }

    public TipoexamenDTO getSelectedTipoexamen() {
        return selectedTipoexamen;
    }

    public void setSelectedTipoexamen(TipoexamenDTO tipoexamen) {
        this.selectedTipoexamen = tipoexamen;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
